* regressions_pgformal_bite_industry.do	JEP	04/05/15
* Does regressions that are in the term paper, for formal workers: pgf
* Monthly wages
* Exclude some industries
* Do regressions by city - industry
* Passthru - Global a formal

*************************** Change log ********************************** 

/*
	
*/	

clear 
version 13.1
set seed 48
* Project info
cap project, doinfo
if _rc==198 {
	if c(os)=="Unix" loc master "/home/jperez/AA_Minimum_Wage" 
 else if c(username)=="J16339" loc master "B:\Col_Minimum_Wage"
 else if c(username)=="A15858" loc master "B:\Col_Minimum_Wage"
	else loc master "C:/Users/jorpp/Dropbox (Brown)/Col_Minimum_Wage"
 loc pr = 0
	cap log close
	log using "`master'/Code/Doc/scale2.log", text replace
}
else {
	local master "`r(pdir)'"
	local doname "`r(dofile)'"  
 loc pr = 1
	* Project calls
	project, uses("`master'/Data/Source/ocupados_mw.dta")
	* Call programs
	project, relies_on("`master'/Code/Est/sample_choice.do")
	project, relies_on("`master'/Code/Est/build_mw_measures.do")
	project, relies_on("`master'/Code/Est/build_treatment.do")
	project, relies_on("`master'/Code/Est/export_rif.do")
	project, relies_on("`master'/Code/Est/confirmedrun.do")
	project, relies_on("`master'/Code/Est/graph_rif.do")
}
*/


* Call necessary programs. I do not call using project to carry the programs around. I do not put these in the ado/personal folder because they are specific to the project. They are not ado files.
do "`master'/Code/Est/sample_choice.do"
do "`master'/Code/Est/build_mw_measures.do"
do "`master'/Code/Est/build_treatment.do"
do "`master'/Code/Est/export_rif.do"
do "`master'/Code/Est/confirmedrun.do"
do "`master'/Code/Est/graph_rif.do"


use "`master'/Data/Source/ocupados_mw.dta", clear

mysample fsample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal exind(13,29,42)



mw_measures fsample , stub(f)
sort id orden time, stable



keep if fsample

keep if time==tq(1998q4)

gen lw=log(salario_mensual_real)
gen lminw=log(minw_mensual_real)
gen lminwf1=log(minw_mensual_real_f1)



* Calculate quantiles of unconditional wage dist
pctile pclw=lw [w=fex] , nq(100)
li pclw in 1/19
* Calculate 
glo at ""
forv i=1(1)19 {
	glo at "$at r(r`i')"
}
kdensity lw [aw=fex], at(pclw) gen (qtau fw_qtau) nograph
li pclw qtau fw_qtau in 1/19

preserve
gen m=_n
keep if pclw!=.
keep pclw fw_qtau qtau m
tempfile pcs
save `pcs'
restore
drop pclw fw_qtau qtau
keep if time==tq(1998q4)
bys ccode cactividad_empresa (ccode cactividad_empresa id orden time): gen m=_n
bys ccode cactividad_empresa (ccode cactividad_empresa id orden time): gen count=_N
merge n:1 m using `pcs',  nogen
sort id orden time, stable

keep if pclw!=.






gen qtaux=.
gen fwx_qtau=.
levelsof ccode, loc(c)
levelsof cactividad, loc(a)
foreach cc in `c' {
	foreach aa in `a' {
		di "`cc' - `aa'"
		kdensity lw [aw=fex] if ccode==`cc' & cactividad==`aa', at(pclw) gen (qtauxtemp fwx_qtautemp) nograph bw(0.2)
		* kdens lw if ccode==`cc' & cactividad==`aa', at(pclw) gen(fwx_qtautemp) nograph bw(0.2)
		* gen qtauxtemp=pclw
		di r(bwidth)
		li pclw qtauxtemp fwx_qtautemp in 1/19
		replace qtaux=qtauxtemp if ccode==`cc' & cactividad==`aa'
		replace fwx_qtau=fwx_qtautemp if ccode==`cc' & cactividad==`aa'
		drop qtauxtemp fwx_qtautemp
	}
} 

* Partial passthrough
gen npclw=.
replace npclw= lminwf1 if pclw`j'>=lminw & pclw`j'<=lminwf1
gen deltapclw = npclw - pclw
gen impliedcoef= deltapclw/bite_f_ind
gen weight=fwx_qtau/fw_qtau
gen wcqe=weight*impliedcoef
* Full passtrough
gen npclwfull=.
replace npclwfull= lminwf1 if pclw`j'<=lminwf1
gen deltapclwfull = npclwfull - pclw
gen impliedcoeffull= deltapclwfull/bite_f_ind
gen wcqefull=weight*impliedcoeffull

collapse (mean) wcqe wcqefull [w=count], by(m)

la var wcqe "Affected"
la var wcqefull "All below"

* replace m=5*m
tostring m, gen(ms)
gen p="p"+ms
labmask m, val(p)
la var m "Percentile"
gen informality=1

tempfile formal
save `formal'



use "`master'/Data/Source/ocupados_mw.dta", clear

mysample fsample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) informal exind(13,29,42)

mw_measures fsample , stub(f)
sort id orden time, stable

keep if fsample
keep if time==tq(1998q4)

gen lw=log(salario_mensual_real)
gen lminw=log(minw_mensual_real)
gen lminwf1=log(minw_mensual_real_f1)

* Calculate quantiles of unconditional wage dist
pctile pclw=lw [w=fex] , nq(100)
* Calculate 
glo at ""
forv i=1(1)19 {
	glo at "$at r(r`i')"
}
kdensity lw [aw=fex], at(pclw) gen (qtau fw_qtau) nograph
preserve
gen m=_n
keep if pclw!=.
keep pclw fw_qtau qtau m
tempfile pcs
save `pcs'
restore
drop pclw fw_qtau qtau
keep if time==tq(1998q4)
bys ccode cactividad_empresa (ccode cactividad_empresa id orden time): gen m=_n
bys ccode cactividad_empresa (ccode cactividad_empresa id orden time): gen count=_N
merge n:1 m using `pcs',  nogen
keep if pclw!=.


sort id orden time, stable

gen qtaux=.
gen fwx_qtau=.
levelsof ccode, loc(c)
levelsof cactividad, loc(a)
foreach cc in `c' {
	foreach aa in `a' {
		kdensity lw if ccode==`cc' & cactividad==`aa' [aw=fex], at(pclw) gen (qtauxtemp fwx_qtautemp) nograph
		replace qtaux=qtauxtemp if ccode==`cc' & cactividad==`aa'
		replace fwx_qtau=fwx_qtautemp if ccode==`cc' & cactividad==`aa'
		drop qtauxtemp fwx_qtautemp
	}
} 

* Partial passthrough
gen npclw=.
replace npclw= lminwf1 if pclw`j'>=lminw & pclw`j'<=lminwf1
gen deltapclw = npclw - pclw
gen impliedcoef= deltapclw/bite_f_ind
gen weight=fwx_qtau/fw_qtau
gen wcqe=weight*impliedcoef
* Full passtrough
gen npclwfull=.
replace npclwfull= lminwf1 if pclw`j'<=lminwf1
gen deltapclwfull = npclwfull - pclw
gen impliedcoeffull= deltapclwfull/bite_f_ind
gen wcqefull=weight*impliedcoeffull

collapse (mean) wcqe wcqefull [w=count], by(m)

la var wcqe "Affected"
la var wcqefull "All below"

* replace m=5*m
tostring m, gen(ms)
gen p="p"+ms
labmask m, val(p)
la var m "Percentile"
gen informality=2

append using `formal'

la var informality "Informality"
la define informality 1 "Formal" 2 "Informal"

ren wcqefull wcqe2
ren wcqe wcqe1
reshape long wcqe, i(m informality)
egen bicho=group(informality _j)

cap la drop bicho
la define bicho 1 "Formal - partial" 2 "Formal - full" 3 "Informal - partial" 4 "Informal - full"
la var bicho "Market and passthrough"
la val bicho bicho

save "`master'/Data/Source/cfac.dta", replace
if `pr' project, creates("`master'/Data/Source/cfac.dta")

/*

* loc master "C:/Users/jorpp/Dropbox Brown/AA_Minimum_Wage"
*glo outdir "`master'/Doc/third_year/tab"
glo outdir "`master'/Doc/tosubmit/tab"
tabout m bicho if m<70 using $outdir/scale.tex, c(mean wcqe) sum f(4c 4c 4c 4c)  ptotal(none) total(none)  replace  topf($outdir/top.tex) botf($outdir/bot.tex)  style(tex) font(bold) bt h3(nil) cl1(2-3 4-5) 
*/

/*
cd $outdir
maketex using scale.tex, packages(booktabs tabularx)
!pdflatex  scale.tex
! scale.pdf
*/

/*
gen id=_n
glo N=_N
forv j=5(5)95	{
	gen npclw`j'=.
	replace npclw`j'=lminwf1 if /* pclw`j'>=lminw & */ pclw`j'<=lminwf1
	gen deltapclw`j'=npclw`j'-pclw`j'
	gen impliedcoef`j' = deltapclw`j' / bite_f_ind
}

*/

